package legumes.client.ui.widgets.list.item;

import legumes.client.ui.widgets.list.ComboOrderedListSimplePanel;
import legumes.client.ui.widgets.list.ComboOrderedListWidget;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Anchor;


public class ComboListItemWidget extends ListItemWidget {

	private ComboOrderedListSimplePanel listWidgetSimplePanel;
	private Anchor anchor;
	
	public ComboListItemWidget(String itemTxt_, ComboOrderedListWidget listWidget_){
		super();
		listWidgetSimplePanel = new ComboOrderedListSimplePanel(listWidget_);
		listWidgetSimplePanel.setVisible(false);
		createComboAnchor( itemTxt_ );
		add(listWidgetSimplePanel);
	}
	
	
	
	@Override
	public void setVisible(boolean visible) {
		anchor.setVisible(visible);
	}

	private void createComboAnchor(String itemTxt_){
		SafeHtmlBuilder shtmlBuilder = new SafeHtmlBuilder();
		shtmlBuilder.appendHtmlConstant("<span class=\"\">" + itemTxt_
				+ "</span><span class=\"arrowCombo\"></span>");
		
		
		anchor = new Anchor(shtmlBuilder.toSafeHtml());
		setAnchorStyleName(anchor);
		anchor.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				onSelectItem();
			}
		});
		add(anchor);
	}
	
	protected void setAnchorStyleName(Anchor anchor_){
	}

	protected void onSelectItem() {
		listWidgetSimplePanel.setVisible(! listWidgetSimplePanel.isVisible() );
		listWidgetSimplePanel.getElement().getStyle().setLeft(
				anchor.getAbsoluteLeft(),
				Unit.PX);
		listWidgetSimplePanel.getElement().getStyle().setTop(
				anchor.getAbsoluteTop() + anchor.getOffsetHeight(),
				Unit.PX);
	}
}
